﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;



namespace BusinessLogic
{
    public class DonDatHangNCC
    {
        #region Vương văn hùng
        public static Entities.DonDatHangNCC[] SelectDonDatHangNCC(Entities.DonDatHangNCC hoadon)
        {
            Entities.DonDatHangNCC[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand("exec sp_Select_DonDatHangNCC @HanhDong,@MaDonDatHangNCC,@MaKho,@MaNhaCungCap", cn);
                cmd.Parameters.Add(Common.Constants.DonDatHangNCC.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.DonDatHangNCC.MaDonDatHangNCC, SqlDbType.VarChar).Value = hoadon.MaDonDatHangNCC;
                cmd.Parameters.Add(Common.Constants.DonDatHangNCC.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                cmd.Parameters.Add(Common.Constants.DonDatHangNCC.MaNhaCungCap, SqlDbType.VarChar).Value = hoadon.MaNhaCungCap;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.DonDatHangNCC row = new Entities.DonDatHangNCC();
                    row.MaDonDatHangNCC = dr[Common.Constants.DonDatHangNCC.MaDonDatHangNCC].ToString();
                    row.NgayDonHang = DateTime.Parse(dr[Common.Constants.DonDatHangNCC.NgayDonHang].ToString());
                    row.MaNhaCungCap = dr[Common.Constants.DonDatHangNCC.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.DonDatHangNCC.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.DonDatHangNCC.NoHienThoi].ToString();
                    row.TrangThaiDonDatHang = dr[Common.Constants.DonDatHangNCC.TrangThaiDonDatHang].ToString();
                    row.NgayNhapDuKien = DateTime.Parse(dr[Common.Constants.DonDatHangNCC.NgayNhapDuKien].ToString());
                    row.HinhThucThanhToan = dr[Common.Constants.DonDatHangNCC.HinhThucThanhToan].ToString();
                    row.MaKho = dr[Common.Constants.DonDatHangNCC.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.DonDatHangNCC.TenKho].ToString();
                    row.MaNhanVien = dr[Common.Constants.DonDatHangNCC.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.DonDatHangNCC.TenNhanVien].ToString();
                    row.MaTienTe = dr[Common.Constants.DonDatHangNCC.MaTienTe].ToString();
                    row.Tygia = dr[Common.Constants.DonDatHangNCC.Tygia].ToString();
                    row.GhiChu = dr[Common.Constants.DonDatHangNCC.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.DonDatHangNCC.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.DonDatHangNCC[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.DonDatHangNCC)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }
        #endregion

        public Entities.DonDatHangNCC[] SelectDonDatHangNCC()
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            Entities.DonDatHangNCC[] ddhncc = null;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangNCC_Select";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "ddhncc");
                dt = ds.Tables["ddhncc"];

                var temp = from ddh in dt.AsEnumerable()
                           select new Entities.DonDatHangNCC
                           {
                               DonDatHangNCCID = ddh.Field<int>("DonDatHangNCCID"),
                               MaDonDatHangNCC = ddh.Field<string>("MaDonDatHangNCC"),
                               NgayDonHang = ddh.Field<DateTime>("NgayDonHang"),
                               MaNhaCungCap = ddh.Field<string>("MaNhaCungCap"),
                               TenNhaCungCap = ddh.Field<string>("TenNhaCungCap"),
                               NoHienThoi = ddh.Field<string>("NoHienThoi"),
                               TrangThaiDonDatHang = ddh.Field<string>("TrangThaiDonDatHang"),
                               NgayNhapDuKien = ddh.Field<DateTime>("NgayNhapDuKien"),
                               HinhThucThanhToan = ddh.Field<string>("HinhThucThanhToan"),
                               MaKho = ddh.Field<string>("MaKho"),
                               TenKho = ddh.Field<string>("TenKho"),
                               MaNhanVien = ddh.Field<string>("MaNhanVien"),
                               TenNhanVien = ddh.Field<string>("TenNhanVien"),
                               MaTienTe = ddh.Field<string>("MaTienTe"),
                               Tygia = ddh.Field<string>("Tygia"),
                               GhiChu = ddh.Field<string>("GhiChu"),
                               Deleted = ddh.Field<bool>("Deleted")
                           };
                ddhncc = temp.ToArray<Entities.DonDatHangNCC>();
            }
            catch
            {
                ddhncc = new Entities.DonDatHangNCC[0];
            }
            finally
            {
                con.Close();
            }
            return ddhncc;
        }
        public int InsertDonDatHangNCC(Entities.DonDatHangNCC ddhncc)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            int i = 0;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangNCC_Insert";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangNCC", SqlDbType.VarChar, 50).Value = ddhncc.MaDonDatHangNCC;
                cmd.Parameters.Add("@NgayDonHang", SqlDbType.DateTime).Value = ddhncc.NgayDonHang;
                cmd.Parameters.Add("@MaNhaCungCap", SqlDbType.VarChar, 50).Value = ddhncc.MaNhaCungCap;
                cmd.Parameters.Add("@TenNhaCungCap", SqlDbType.NVarChar, 200).Value = ddhncc.TenNhaCungCap;
                cmd.Parameters.Add("@NoHienThoi", SqlDbType.Float).Value = ddhncc.NoHienThoi;
                cmd.Parameters.Add("@TrangThaiDonDatHang", SqlDbType.NVarChar, 20).Value = ddhncc.TrangThaiDonDatHang;
                cmd.Parameters.Add("@NgayNhapDuKien", SqlDbType.DateTime).Value = ddhncc.NgayNhapDuKien;
                cmd.Parameters.Add("@HinhThucThanhToan", SqlDbType.NVarChar, 20).Value = ddhncc.HinhThucThanhToan;
                cmd.Parameters.Add("@MaKho", SqlDbType.VarChar, 50).Value = ddhncc.MaKho;
                cmd.Parameters.Add("@TenKho", SqlDbType.NVarChar, 200).Value = ddhncc.TenKho;
                cmd.Parameters.Add("@MaNhanVien", SqlDbType.VarChar, 50).Value = ddhncc.MaNhanVien;
                cmd.Parameters.Add("@TenNhanVien", SqlDbType.NVarChar, 200).Value = ddhncc.TenNhanVien;
                cmd.Parameters.Add("@MaTienTe", SqlDbType.VarChar, 50).Value = ddhncc.MaTienTe;
                cmd.Parameters.Add("@Tygia", SqlDbType.NVarChar, 200).Value = ddhncc.Tygia;
                cmd.Parameters.Add("@GhiChu", SqlDbType.VarChar, 50).Value = ddhncc.GhiChu;
                cmd.Parameters.Add("@Deleted", SqlDbType.Bit).Value = ddhncc.Deleted;

                i = cmd.ExecuteNonQuery();
                LuuVietHeThong.LuuViet("Insert", ddhncc.TaiKhoan.MaNhanVien, "Thêm đơn đặt hàng: " + ddhncc.MaDonDatHangNCC, ddhncc.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {
                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }
        public int DeleteDonDatHangNCC(Entities.DonDatHangNCC ddhncc)
        {
            SqlConnection con = DataAccess.Connection.OpenConnection();
            DataTable dt = new DataTable();
            int i = 0;
            try
            {
                 
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "sp_DonHangNCC_Delete";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;

                cmd.Parameters.Add("@MaDonDatHangNCC", SqlDbType.VarChar, 50).Value = ddhncc.MaDonDatHangNCC;
                

                i = cmd.ExecuteNonQuery();
                LuuVietHeThong.LuuViet("Delete", ddhncc.TaiKhoan.MaNhanVien, "Xóa đơn đặt hàng: " + ddhncc.MaDonDatHangNCC, ddhncc.TaiKhoan.TenDangNhap, DateTime.Now.ToString("hh:mm dd/MM/yyyy"));
            }
            catch
            {
                i = -2;
            }
            finally
            {
                con.Close();
            }
            return i;
        }

    }
}
